package com.tuoyu.peopletest.utils;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;

import com.tuoyu.peopletest.common.MyApplication;
import com.tuoyu.peopletest.entity.message.MyMessageEvent;
import com.tuoyu.peopletest.view.ac.BaseActivity;
import com.tuoyu.peopletest.view.ac.MainActivity;

import org.greenrobot.eventbus.EventBus;


/**
 *
 */
public class SCAppUtils {


    /**
     * 获取软件版本号
     *
     * @param
     */
    public static int getVersionCode() {
        int versionCode = 0;
        try {
            // 获取软件版本号，对应AndroidManifest.xml下android:versionCode
            PackageInfo info = MyApplication.context.getPackageManager()
                    .getPackageInfo( MyApplication.context.getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return versionCode;
    }

    public static String getEncode(String conent) {
        // LogUtils.e(TAG,"编码的数据conent = "+conent);
        //LogUtils.e(TAG,"编码的Base64 = "+Base64.encodeToString(conent.getBytes(), Base64.DEFAULT));
        if (TextUtils.isEmpty(conent)) {
            return "";
        }
        return Base64.encodeToString(conent.getBytes(), Base64.DEFAULT);
    }


    public static String getDecode(String conent) {
        //LogUtils.e(TAG,"解码的数据conent = "+conent);
        //LogUtils.e();
        //byte[] decode = Base64.decode(conent.getBytes(), Base64.DEFAULT);

        //LogUtils.e(TAG,"解码的Base64 = "+new String(decode));
        if (TextUtils.isEmpty(conent)) {
            return "";
        }
        return new String(Base64.decode(conent.getBytes(), Base64.DEFAULT));
    }


    /**
     * 去打电话
     */
    public static void goCallPhone(Context context, String phoneNum) {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNum));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static String getPhoneType() {
        //手机型号
        String brand = Build.MODEL;

        return brand;
    }

    public static String getPhoneBrand() {
        //品牌
        String brand = Build.BRAND;
        return brand;
    }

    public static void sendCloseMain(){
        EventBus.getDefault().post(new MyMessageEvent().setCloseMainAc(true));
    }

    /**
     * 因为单一任务模式,会产生bug, 所以跳转主页要先关闭存在的,
     * @param ac
     */
    public static void start2MainAc(BaseActivity ac){
        sendCloseMain();
        Intent intent = new Intent(ac, MainActivity.class);
        ac.startActivity(intent);
    }


}
